package com.penghaisoft.microwms.base.web.auth.service.impl;

import com.penghaisoft.microwms.base.web.auth.service.IRoleService;
import com.penghaisoft.microwms.base.web.resrole.dao.BaseResRoleMapper;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.base.model.BaseResRole;
import com.penghaisoft.microwms.common.base.model.BaseResRoleFactory;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.BaseService;
import com.penghaisoft.microwms.common.util.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Title: RoleServiceImpl.java
 * @Description: 用户管理服务实现
 * @author qh
 * @date 2017/3/1 16:24
 * @version V0.5
 */
@Service
public class RoleServiceImpl extends BaseService<BaseResRole> implements IRoleService {

	@Autowired
	private BaseResRoleMapper roleMapper;

	/**
	 * @Description: 根据id查询资源角色
	 * @author qh
	 * @date 2017/3/1 16:24
	 * @param  id (资源角色Id)
	 * @return BaseResRol (资源角色对象)
	 */
	@Override
	public BaseResRole queryById(String id) {
		BaseResRole resrole = roleMapper.selectByPrimaryKey(id);
		return resrole;
	}

	/**
	 * @Description: 添加资源角色
	 * @author qh
	 * @date 2017/3/1 16:24
	 * @param role (资源角色对象)
	 * @return Resp (结果对象)
	 */
	@Override
	public Resp add(BaseResRole role) {
		// 重复性校验
		int count = roleMapper.checkUnique(role);
		Resp resp = new Resp();
		resp.setCode(Constant.SUCCESS);
		if (count != 0) {
			resp.setCode(Constant.FAIL);
			resp.setMsg("base.auth.role.name.conflict");
		} else {
			// 真正插入
			String id = CommonUtils.getUUID();
			role.setResroleId(id);
			role.setActiveFlag("1");
			roleMapper.insertSelective(role);
		}
		return resp;
	}

	/**
	 * @Description: 修改资源角色信息
	 * @author qh
	 * @date 2017/3/1 16:24
	 * @param role(资源角色对象)
	 * @return Resp (结果对象)
	 */
	@Override
	public Resp update(BaseResRole role) {
		// 重复性校验
		int count = roleMapper.checkUnique(role);
		Resp resp = new Resp();
		resp.setCode(Constant.SUCCESS);
		if (count != 0) {
			resp.setCode(Constant.FAIL);
			resp.setMsg("base.auth.role.name.conflict");
		} else {
			// 更新
			roleMapper.updateByPrimaryKeySelective(role);
		}
		return resp;
	}

	/**
	 * @Description: 删除资源角色，支持批量
	 * @author qh
	 * @date 2017/3/1 16:24
	 * @param ids (资源角色Id列表)
	 * @return Resp (结果对象)
	 */
	@Override
	public Resp deleteRoles(List<String> ids) {
		Resp resp = new Resp();
		resp.setCode(Constant.SUCCESS);
		int count = roleMapper.deleteByPrimaryKeys(ids);
		if (count == 0) {
			resp.setCode(Constant.FAIL);
			resp.setMsg("base.auth.role.delete.notexist");
		}
		return resp;
	}

	/**
	 * @Description: 查询资源角色列表
	 * @author qh
	 * @date 2017/3/1 16:24
	 * @param page, rows, condition
	 * @return List<BaseResRole> (资源角色列表)
	 */
	@Override
	public Pager<BaseResRole> queryList(int page, int rows, BaseResRole condition) {
		Pager<BaseResRole> pager = new Pager<>();
		pager.setPage(page);
		pager.setRows(rows);
		condition.setPageSize(rows);
		condition.setCurrentPage(page);
		List<BaseResRole> resRoles = roleMapper.selectListByCondition(condition);
		int totalCount = roleMapper.selectCountByCondition(condition);
		pager.setRecords(resRoles);
		pager.setTotalCount(totalCount);
		return pager;
	}
	
	/**
	 * @Description: 获取所有角色的信息
	 * @author lt
	 * @date 2017/3/17 10:33
	 * @param  condition
	 * @return List<BaseResRole> (角色列表)
	 */
	@Override
	public List<BaseResRole> queryAllRoleList(BaseResRole condition) {
		List<BaseResRole> rtList = roleMapper.selectAllRoleList(condition);
		return rtList;
	}
	/**
	 * @Description: 获取所有角色的工厂
	 * @author jzh
	 */
	@Override
	public List<BaseResRoleFactory> queryAllFactory(BaseResRoleFactory condition) {
		return roleMapper.queryAllFactory(condition);
	}
	/** 
	 * @Title RoleResFactory
	 * @Description 更新角色下工厂
	 * @author jzh
	 */
	@Transactional
	@Override
	public Resp RoleResFactory(List<BaseResRoleFactory> list) {
		// 1.删除原来的角色工厂
		roleMapper.deleteAllFactory(list.get(0));
		if (!list.get(0).getResroleId().equals("")) {
			// 2.新增新工厂
			roleMapper.insertFactoryResroleBatch(list);
		}
		Resp resp = new Resp();
	    resp.setCode(Constant.SUCCESS);
		return resp;
	}

}
